package imPro;

import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;

/**
 *
 * @author Ragnar
 */
public class CamCapture implements Runnable
{
    private CaptureDeviceInfo di;
    private MediaLocator ml;
    private Player player;
    private Common source;
    
    public CamCapture(Common com)
    {
        this.source = com;
        
        String str = "vfw:Microsoft WDM Image Capture (Win32):0";
        di = CaptureDeviceManager.getDevice(str);
        ml = new MediaLocator(str);
        try{
            player = Manager.createRealizedPlayer(ml);
            player.start();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        Thread t = new Thread(this);
        t.start();
    }
    
    public void run()
    {
        while (true)
        {
            FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
            Buffer b = fgc.grabFrame();
            BufferToImage btoi = new BufferToImage((VideoFormat) b.getFormat());
            this.source.setImage(btoi.createImage(b));

            try
            {
                Thread.sleep(50);
            }
            catch (InterruptedException e) {}
        }
    }
}
